package com.example.zengpengcheng0526;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class GuangBo extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean flag = intent.getBooleanExtra("flag", false);
        String msg = intent.getStringExtra("msg");
        User gbuser = (User) intent.getSerializableExtra("gbuser");
        //String ss = intent.getStringExtra("ss");
       // Log.d( "onReceive: ",ss);
        if (flag){
            //保存账号
            SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = sp.edit();
            edit.putString("name",gbuser.getUsername());
            edit.commit();
            //保存密码
            try {
                FileOutputStream outputStream = context.openFileOutput("user.txt", Context.MODE_PRIVATE);
                outputStream.write(gbuser.getPw().getBytes());
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            context.startActivity(new Intent(context,MainActivity.class));
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }
}
